package com.yonyou.pmclouds.trialinfo.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.conf.WorkbenchDataSourceProperties;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.productactive.entity.BuziOrderInfoVO;
import com.yonyou.pmclouds.productactive.process.PrintInitProcess;
import com.yonyou.pmclouds.productactive.process.ScriptProcess;
import com.yonyou.pmclouds.productactive.rmiitf.ProductActiveRemoteService;
import com.yonyou.pmclouds.trialinfo.entity.TrialInfoVO;
import com.yonyou.pmclouds.trialinfo.rmiitf.TrialInfoMaintain;
import com.yonyou.pmclouds.trialinfo.mapper.TrialInfoMapper;
import com.yonyou.pmclouds.trialinfo.processor.TrialInsertBeforeProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

@Service(version = ApplicationConsts.APPLICATION_VERSION ,interfaceClass = TrialInfoMaintain.class)
public class TrialInfoMaintainImpl implements TrialInfoMaintain {

    @Autowired
    private TrialInfoMapper mapper;
    @Autowired
    private WorkbenchDataSourceProperties properties;
    @Autowired
    private ProductActiveRemoteService activeService;

    @Override
    public TrialInfoVO insertTrialInfoVO(TrialInfoVO trialInfoVO) throws BusinessException {


        SingleInsertBpTemplate<TrialInfoVO> bp = new SingleInsertBpTemplate<TrialInfoVO>(mapper);
        bp.addBeforeProcessor(new TrialInsertBeforeProcessor());
        trialInfoVO = bp.insert(trialInfoVO);

        return trialInfoVO;
    }

    @Override
    public TrialInfoVO approveTrialInfoVO(TrialInfoVO trialInfoVO) throws BusinessException {

        String[] updateFieldByApprove = new String[]{CommonFieldConst.BILLSTATUS,
                TrialInfoVO.APPROVETIME,
                TrialInfoVO.APPROVER,
                TrialInfoVO.PKTENANT,
                TrialInfoVO.ENDTIME,
                TrialInfoVO.USERID};

        SingleUpdateBpTemplate<TrialInfoVO> bp = new SingleUpdateBpTemplate<TrialInfoVO>(mapper,
                updateFieldByApprove);
        trialInfoVO = bp.update(trialInfoVO);

        insertActiveInfo(trialInfoVO);

        ScriptProcess process = new ScriptProcess(properties);
        process.executeScript(trialInfoVO.getPkTenant(), ApplicationConsts.APPLICATION_ID);

        //初始化一套业务对象和打印模版
        PrintInitProcess printInitProcess = new PrintInitProcess(trialInfoVO.getPkTenant());
        printInitProcess.initTenantBizz();
        return trialInfoVO;
    }

    /**
     * 记录试用开通信息
     *
     * @param trialInfoVO
     * @throws BusinessException
     */
    private void insertActiveInfo(TrialInfoVO trialInfoVO) throws BusinessException {
        BuziOrderInfoVO buziInfo = new BuziOrderInfoVO();
        buziInfo.setPkTenant(trialInfoVO.getPkTenant());
        buziInfo.setAccountQuantity(trialInfoVO.getAccountQuantity());
        buziInfo.setLease(trialInfoVO.getLease());
        buziInfo.setProductId(trialInfoVO.getProductId());
        buziInfo.setChangeFlag(UUID.randomUUID().toString());
        buziInfo.setActiveDate(trialInfoVO.getApprovetime());

        buziInfo.setPurType((byte) 3);

        activeService.insertOrderInfo(buziInfo);
    }


}
